//
//  LoadingHeadPage.m
//  Bracelet
//
//  Created by steve on 15-3-19.
//  Copyright (c) 2015年 jing zhao. All rights reserved.
//

#import "LoadingHeadPage.h"

@interface LoadingHeadPage()

@property (strong, nonatomic) NSMutableArray *		m_arrayButton;
@property (assign, nonatomic) NSInteger            m_intWantGoToPage;

@end

@implementation LoadingHeadPage

-(void)initButtonArrayWithNum:(NSInteger)num
{
  
  _m_arrayButton = [[NSMutableArray alloc]init];
  NSInteger bothMarginLength = (150 -(2*num-1)*10)/2;
  NSInteger buttonWidth = 10;
  NSInteger lengthBetweenButtons=10;
  for (int i=0; i<num; i++) {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(bothMarginLength+(buttonWidth+lengthBetweenButtons)*i, 5, buttonWidth, buttonWidth);
    [btn addTarget:self action:@selector(pageBtnClicked:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:btn];
    [_m_arrayButton addObject:btn];
  }
  
  self.m_intWantGoToPage = 0;
  [self swithTo:[self.m_arrayButton objectAtIndex:0]
       switched:^(int _index) {}];
}

-(void)setHeaderPageChangGoToButtonIndex:(NSInteger)_buttonIndex
{
  
  if (_buttonIndex <0 || _buttonIndex >[self.m_arrayButton count]-1|| _buttonIndex ==self.m_intWantGoToPage)
  {
    return;
  }
  self.m_intWantGoToPage = _buttonIndex;
  [self swithTo:[self.m_arrayButton objectAtIndex:_buttonIndex]
       switched:^(int _index) {}];
}

typedef void (^KUISwitchHeadPage_Swith)(int _index);

- (void)swithTo:(UIButton *)_buttonSender
       switched:(KUISwitchHeadPage_Swith)_blockSwitch
{
  
  for (UIButton *button in self.m_arrayButton )
  {
    [button setBackgroundImage:[UIImage imageNamed:@"pageBgd2"] forState:UIControlStateNormal];
  }
  
  [_buttonSender setBackgroundImage:[UIImage imageNamed:@"pageBgd1"] forState:UIControlStateNormal];
  
}

-(void)pageBtnClicked:(id)sender{
  UIButton *btn = (UIButton *)sender;
  NSInteger _index	=	[self.m_arrayButton indexOfObject:btn];
  [self.m_delegate delegateOnHeadPageSwitchButtonTap:_index];
  [self setHeaderPageChangGoToButtonIndex:_index];
}

@end
